home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTE23 / TIMER.C < prev    next >
C/C++ Source or Header  |  1996-04-29  |  7KB  |  262 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "Timer.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "My Application"; 
  22.  
  23. // the rest of the stuff
  24. //......................
  25.  
  26. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.     WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );
  107. }
  108.  
  109.  
  110.  
  111. LRESULT CALLBACK About( HWND hDlg,           
  112.                         UINT message,        
  113.                         WPARAM wParam,       
  114.                         LPARAM lParam)
  115. {
  116.    switch (message) 
  117.    {
  118.        case WM_INITDIALOG: 
  119.                return (TRUE);
  120.  
  121.        case WM_COMMAND:                              
  122.                if (   LOWORD(wParam) == IDOK         
  123.                    || LOWORD(wParam) == IDCANCEL)    
  124.                {
  125.                        EndDialog(hDlg, TRUE);        
  126.                        return (TRUE);
  127.                }
  128.                break;
  129.    }
  130.  
  131.    return (FALSE); 
  132. }
  133.  
  134.  
  135. #define USR_TIMER_ELAPSED  (WM_USER+1)
  136.  
  137. BOOL CALLBACK TimerDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  138. VOID CALLBACK timeProc(UINT nTimerId, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2);
  139.  
  140.  
  141. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  142. {
  143.    switch( uMsg )
  144.    {
  145.       case WM_COMMAND :
  146.               switch( LOWORD( wParam ) )
  147.               {
  148.                  case IDM_TEST:
  149.                         DialogBox(hInst, "TimerDlg", hWnd, TimerDlg);
  150.                         break;
  151.  
  152.                  case IDM_ABOUT :
  153.                         DialogBox( hInst, "AboutBox", hWnd, About );
  154.                         break;
  155.  
  156.                  case IDM_EXIT :
  157.                         DestroyWindow( hWnd );
  158.                         break;
  159.               }
  160.               break;
  161.  
  162.       case WM_DESTROY :
  163.               PostQuitMessage(0);
  164.               break;
  165.  
  166.       default :
  167.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  168.    }
  169.  
  170.    return( 0L );               
  171. }
  172.  
  173.  
  174. BOOL CALLBACK TimerDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  175. {
  176.    static UINT nTimerId;
  177.    static UINT nMs;
  178.    static UINT nEvents;  // number of timer events.
  179.  
  180.    switch (uMsg)
  181.     {
  182.         // init dialog
  183.         //.............
  184.         
  185.         case WM_INITDIALOG:
  186.                {
  187.                   TIMECAPS tc;
  188.  
  189.                   nTimerId = 0;
  190.                   nEvents  = 0;
  191.                   nMs      = 500;
  192.  
  193.                   timeGetDevCaps(&tc, sizeof(TIMECAPS) );
  194.  
  195.                   if (nMs < tc.wPeriodMin)
  196.                       nMs = tc.wPeriodMin;
  197.                   else if (nMs > tc.wPeriodMax)
  198.                       nMs = tc.wPeriodMax;
  199.  
  200.                   timeBeginPeriod(nMs);
  201.  
  202.                   nTimerId = timeSetEvent(nMs, nMs, timeProc, (DWORD)hDlg,
  203.                                           TIME_PERIODIC);
  204.  
  205.                   if (!nTimerId)
  206.                   {                                            
  207.                       timeEndPeriod(nMs);
  208.                       MessageBox(hDlg, "Error setting up timer...", NULL, MB_OK);
  209.                       EndDialog(hDlg, FALSE);
  210.                       return(TRUE);
  211.                   }
  212.  
  213.                }
  214.                return(TRUE);
  215.  
  216.         case USR_TIMER_ELAPSED :
  217.                nEvents++;
  218.                SetDlgItemInt(hDlg, IDC_TIMER_EC, nEvents, FALSE);
  219.                break;
  220.  
  221.         // commands
  222.         //.........
  223.  
  224.         case WM_COMMAND:
  225.                switch ( LOWORD(wParam) )
  226.                {
  227.                   // close down
  228.                   //...........
  229.  
  230.                   case WM_CLOSE:
  231.                   case IDOK:
  232.                   case IDCANCEL:
  233.                          {
  234.                             // Shut down timer
  235.                             //................
  236.  
  237.                             timeKillEvent(nTimerId);
  238.                             timeEndPeriod(nMs);
  239.                             
  240.                             EndDialog(hDlg, TRUE);
  241.                          }
  242.                          break;
  243.                }
  244.                break;
  245.  
  246.     }
  247.  
  248.     return (FALSE);
  249. }
  250.  
  251.  
  252. VOID CALLBACK timeProc(UINT nTimerId, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
  253. {
  254.    PostMessage((HWND)dwUser, USR_TIMER_ELAPSED, 0, 0);
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.